var cflz_service_url = "/netcflz_wx/service/app_data_action.ashx";
var jsonUser, jsonDoc, jsonShop;

//请求数据,strMsgTag数据名称，pJson，callBack
function GetPageData(strMsgTag, pJson, callBack) {
    $.showLoading();
    var dataJson = { "rand": Math.random() };
    if (!isSpace(pJson)) {
        for (var key in pJson) {
            dataJson[key] = pJson[key];
        }
    }
    var ajaxArgs = { url:cflz_service_url,
        data: dataJson,
        succCallback: callBack,
        type:"post",
        errorCallback: function (errorMsg) {
            alert(errorMsg);
            showJSZ("程序错误，请联系管理员");

        }
    };
    $ajax(ajaxArgs);
}
//ajax函数二次封装
function $ajax(ajaxArgs) {
    var bAsync = ajaxArgs.async || false; //异步请求
    var strDataType = ajaxArgs.dataType || 'json'; //接收数据类型
    var strType = ajaxArgs.type || 'get'; //请求类型
    var bLoad = ajaxArgs.isLoad || false; //是否显示loading动画
    var bLoadParent = ajaxArgs.isParent || false; //显示父页loading动画
    var strLoadText = ajaxArgs.loadText || ''; //显示内容
    var succCallback = ajaxArgs.succCallback; //成功回调函数
    var errorCallback = ajaxArgs.errorCallback; //失败回调函数    
    $.ajax({
        async: bAsync,
        url: ajaxArgs.url,
        data: ajaxArgs.data,
        dataType: strDataType,
        type: strType,
        beforeSend: function () {//开始loading
            if (bLoad) { }
        },
        success: function (rstData) {
            $.hideLoading();
            if ($.isFunction(succCallback))
                succCallback(rstData);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $.hideLoading();
            if ($.isFunction(errorCallback))
                errorCallback(textStatus + "\r\n" + errorThrown);
        },
        complete: function () {//结束loading
            if (bLoad) { }
        }
    });
}

//判断字符串为空
function isSpace(strMain) {
    var strComp = strMain;
    try {
        if (strComp == "　" || strComp == "" || strComp == " " || strComp == null || strComp == "null" || strComp == "0" || strComp.length == 0 || typeof strMain == "undefined" || strMain == "undefined") {
            return true;
        }
        else {
            return false;
        }
    }
    catch (e) {
        return false;
    }
}
//后退或前进
function actPageGo(count) {
    history.go(count);
}
//页面跳转
function actToPage(url) {
    location.href = url;
}
//获取URL参数
function getQst(name) {
    var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return decodeURI(result[1]);
}
//过滤HTML标签
function filterHTMLTag(str) {
    var msg = str.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    msg = msg.replace(/[|]*\n/, ''); //去除行尾空格
    msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
}
//弹窗
function showJSZ(msg) {
    if (msg == "") msg = "建设中……";
    $.toast.prototype.defaults.duration = 1000; //1秒
    $.toast(msg, 'text');
}
/**
 * 生成二维码
 * @param {any} obj
 * @param {any} width
 * @param {any} height
 */
function getQCCode(ID, obj, width, height) {
    var qrcode = new QRCode(document.getElementById(ID), {
        width: width,
        height: height
    });
    var elText = obj;
    if (!elText.value) {
        elText.focus();
        return;
    }
    qrcode.makeCode(elText.value);
}

///验证身份证
function IsIdCard(idcard) {
    //var Errors = new Array("验证通过!", "身份证号码位数不对!", "身份证号码出生日期超出范围或含有非法字符!", "身份证号码校验错误!", "身份证地区非法!");
    var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁",
        22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建",
        36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西",
        46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西",
        62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门",
        91: "国外"
    }
    idcard = idcard.toUpperCase();
    var Y, JYM; var S, M; var idcard_array = new Array(); idcard_array = idcard.split("");
    //地区检验
    if (area[parseInt(idcard.substr(0, 2))] == null) {
        return false;
    }
    //身份号码位数及格式检验
    switch (idcard.length) {
        case 15: if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0))
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; //测试出生日期的合法性
            else ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; //测试出生日期的合法性
            if (ereg.test(idcard)) break;
            else { return false; } break;
        case 18:
            //18位身份号码检测
            //出生日期的合法性检查 
            //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
            //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
            if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0))
                ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; //闰年出生日期的合法性正则表达式
            else ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; //平年出生日期的合法性正则表达式
            if (ereg.test(idcard)) {//测试出生日期的合法性
                //计算校验位
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 +
                (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 +
                (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 +
                (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 +
                (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 +
                (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 +
                (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 +
                parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 +
                parseInt(idcard_array[9]) * 3;
                Y = S % 11; M = "F"; JYM = "10X98765432"; M = JYM.substr(Y, 1); //判断校验位
                if (M == idcard_array[17]) break; //检测ID的校验位
                else { return false; }
            } else { return false; } break;
        default: return false; break;
    }
    return true;
}


//判断一个变量是否是日期型
function isDate(sDate) {
    var arrDate = sDate.split("-");
    if (arrDate.length != 3) return false;

    var year = arrDate[0];
    var month = arrDate[1];
    var day = arrDate[2];

    if (isEmpty(year) || isEmpty(month) || isEmpty(day)) return false;
    if (isNaN(year) || isNaN(month) || isNaN(day)) return false;

    year = parseInt(LTrimZero(year));
    month = parseInt(LTrimZero(month));
    day = parseInt(LTrimZero(day));

    if (year < 0 || month > 12 || day > 31 || month <= 0 || day <= 0) return false;

    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return true;
        case 4:
        case 6:
        case 9:
        case 11:
            if (day > 30) return false;
            return true;
        case 2:

            //闰年
            if (0 == year % 4 && (year % 100) != 0 || 0 == year % 400) {
                if (day > 29) return false;
                return true;
            }
            //非闰年 
            if (day > 28) return false;
            return true;
        default:
            return false;
    }
}


//判断一个字符串是否为空字符串
function isEmpty(s) {
    return "" == Trim(s);
}



/*=============== 数据处理函数 ===========*/
function LTrim(s) {
    for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) != " ") {
            return s.substr(i, s.length - i);
        }
    }
    return "";
}

function RTrim(s) {
    for (var i = s.length - 1; i >= 0; i--) {
        if (s.charAt(i) != " ") {
            return s.substr(0, i + 1);
        }
    }
    return "";
}

function Trim(s) {
    if (typeof s == "undefined" || s == "undefined")
        s = "";
    else
        s = RTrim(LTrim(s));
    return s;
}


function LTrimZero(s) {
    for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) != "0") {
            return s.substr(i, s.length - i);
        }
    }
    return "";
}

//验证证件号 并把生日, 性别赋值
function checkidno(idno, objbir, strrbl_sex) {
    var nian, yue, ri, csrq, xingbie, s;
    var s = idno.value;
    if (!IsIdCard(s)) {
        showJSZ("请输入正确的身份证号码！");
        return false;
    }
    //身份证为15位
    if (s.length == 15) {
        nian = s.substr(6, 2);
        yue = s.substr(8, 2);
        ri = s.substr(10, 2);
        csrq = "19" + nian + "-" + yue + "-" + ri;
        xingbie = s.charAt(14);
        if (xingbie % 2 == 0) {
            ClickMe(strrbl_sex, 2);
        }
        else {
            ClickMe(strrbl_sex, 1);
        }
        if (isDate(csrq)) {
            objbir.value = csrq;
        }
        else {
            showJSZ("请输入正确的身份证号码！");
            return false;
        }
    }
    //身份证为18位
    else if (s.length == 18) {
        nian = s.substr(6, 4);
        yue = s.substr(10, 2);
        ri = s.substr(12, 2);
        csrq = nian + "-" + yue + "-" + ri;
        xingbie = s.charAt(16);
        if (xingbie % 2 == 0) {
            ClickMe(strrbl_sex, 2);
        }
        else {
            ClickMe(strrbl_sex, 1);
        }
        if (isDate(csrq)) {
            objbir.value = csrq;
        }
        else {
            showJSZ("请输入正确的身份证号码！");
            return false;
        }
    }
    else {
        showJSZ("身份证号必需要15位或18位！");
        return false;
    }
    return true;
}


///绑定单选控件
function ClickMe(name, value) {
    $("input[name=" + name + "]").each(function () {
        if ($(this).val() != value) {
            $(this).removeAttr("checked");
        }
        else {
            $(this).prop("checked", "checked");
        }
    });
}


//绑定多选控件
function ClickMeMore(name, values) {
    var strValue, strValues = "," + values + ",";
    $("input[name=" + name + "]").each(function (i) {
        strValue = "," + $(this).val() + ",";
        if (strValues.indexOf(strValue) < 0) {
            $(this).removeAttr("checked");
        }
        else {
            $(this).prop("checked", "checked");
        }
    });
}

function BindSelectData(ctrlId, tbName) {
    var str_item = "";
    GetPageData('获取字典', { "actName": "Doctor", "actTag": "D999", "tbName": tbName }, function (rstData) {
        $("#text_" + ctrlId).select({
            title: "",
            items: rstData,
            onChange: function (d) {
                $("#hid_" + ctrlId).val($("#text_" + ctrlId).attr("data-values"));
                console.log(this, d);
            },
            onClose: function () {
                console.log("close");
            },
            onOpen: function () {
                console.log("open");
            }
        });
    });
}

function SetFormCtls(rstData) {
    var str_name, str_key;
    $("input").each(function () {
        str_name = $(this).attr('id');
        if (!isSpace(str_name) && ((str_name.indexOf("hid_") == 0) || (str_name.indexOf("txt_") == 0) || (str_name.indexOf("ckb_") == 0))) {
            str_key = str_name.substring(4);
            if (str_key in rstData)
                $('#' + str_name).attr('value', rstData[str_key]);
        }
        if (!isSpace(str_name) && (str_name.indexOf("ckb_") == 0)) {
            str_key = str_name.substring(4);
            if (str_key in rstData && rstData[str_key] == '1')
                $('#' + str_name).attr('checked', true);
        }
    });
    $("select").each(function () {
        str_name = $(this).attr('id');
        if (!isSpace(str_name) && str_name.indexOf("ddl_") == 0) {
            str_key = str_name.substring(4);
            if (str_key in rstData)
                $('#' + str_name).attr('value', rstData[str_key]);
        }
    });
    $("textarea").each(function () {
        str_name = $(this).attr('id');
        if (!isSpace(str_name) && str_name.indexOf("txt_") == 0) {
            str_key = str_name.substring(4);
            if (str_key in rstData)
                $('#' + str_name).html(rstData[str_key]);
        }
    });
}

//ctrlId点击此控件弹出全屏, pid为a标签id
function OpenPopup(ctrlId,pid) {
    $("#" + ctrlId).focus(function () {
        $("#a_show"+pid).click();
    });
}
//ctrlId显示中文的控件, ctrlHidId记录code的隐藏域, pid为a标签id
function ClosePopup(ctrlId, ctrlHidId, pid, icd_code, icd_name) {
    $("#" + ctrlHidId).val(icd_code);
    $("#" + ctrlId).val(icd_name);
    $("#a_close" + pid).click();
}
//ctrlId显示中文的控件, ctrlHidId记录code的隐藏域, pid为a标签id,divId
function LoadPopupData(pJson, ctrlId, ctrlHidId, pid, divId) {
    var dataJson = { "actName": "Doctor", "actTag": "Get_ICD" };
    if (!isSpace(pJson)) {
        for (var key in pJson) {
            dataJson[key] = pJson[key];
        }
    }
    GetPageData('', dataJson, function (rstData) {
        if (rstData.total > 0) {
            var str_cells = "";
            for (var i = 0; i < rstData.total; i++) {
                str_cells += "<div class=\"weui-cells\">";
                str_cells += "<a class=\"weui-cell weui-cell_access\" href=\"javascript:ClosePopup('" + ctrlId + "','" + ctrlHidId + "','" + pid + "','" + rstData.rows[i].value + "','" + rstData.rows[i].content + "')\">";
                str_cells += "<div class=\"weui-cell__bd\">";
                str_cells += "<p>" + rstData.rows[i].content + "</p>";
                str_cells += "</div>";
                str_cells += "<div class=\"weui-cell__ft\">";
                str_cells += "</div>";
                str_cells += "</a>";
                str_cells += "</div>";
            }
            $("#" + divId).html(str_cells);
        }
        else { $("#" + divId).html("无查询结果"); }
    });
}


///判断非空
function isSpace(strMain) {
    var strComp = strMain;
    try {
        if (strComp == "　" || strComp == "" || strComp == " " || strComp == null || strComp == "null" || strComp == "0" || strComp.length == 0 || typeof strMain == "undefined" || strMain == "undefined") {
            return true;
        }
        else {
            return false;
        }
    }
    catch (e) {
        return false;
    }
}